<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>JavaScripTools Manual</title>
    <link rel="stylesheet" type="text/css" href="../../css/style.css">
</head>

<body>
<br>
<h1>JavaScripTools Manual</h1>
<h2>Parsers</h2>
<h3>Working with the NumberParser</h3>
<p>The 
<a href="../api/class_NumberParser.html">NumberParser</a>
is a class that can convert strings to numbers and numbers to strings using
custom formatting options, like decimal and grouping separator, fixed amount of
decimal digits, currency symbol and using parenthesis for negative numbers instead
of a minus sign.</p>
<p>All those options can be set in 3 ways:<ul>
<li>Redefining the default value "constants". See <a href="General_customizing.html">this chapter</a>
for more details. The constants will be explain together with their respective related properties</li>
<li>Passing the property values on the constructor</li>
<li>Setting the properties on the NumberParser instance</li>
</ul></p>
<p>The availiable properties are the following, presented in the constructor parameters order:</p>
<table width="100%" cellspacing="0" border="1">
    <tr>
        <th align="left" width="15%">Property</th>
        <th align="left" width="15%">Default value constant</th>
        <th align="left">Description</th>
    </tr>
    <tr>
        <td>decimalDigits</td>
        <td>JST_DEFAULT_DECIMAL_DIGITS</td>
        <td>The number of decimal digits (-1 Means no limit). When the number of digits is reduced, the standard round operation is used</td>
    </tr>
    <tr>
        <td>decimalSeparator</td>
        <td>JST_DEFAULT_DECIMAL_SEPARATOR</td>
        <td>The decimal separator</td>
    </tr>
    <tr>
        <td>groupSeparator</td>
        <td>JST_DEFAULT_GROUP_SEPARATOR</td>
        <td>The separator between integer groups (normally thousands)</td>
    </tr>
    <tr>
        <td>useGrouping</td>
        <td>JST_DEFAULT_USE_GROUPING</td>
        <td>A flag indicating if the grouping separator be used</td>
    </tr>
    <tr>
        <td>currencySymbol</td>
        <td>JST_DEFAULT_CURRENCY_SYMBOL</td>
        <td>A string representing the currency symbol</td>
    </tr>
    <tr>
        <td>useCurrency</td>
        <td>JST_DEFAULT_USE_CURRENCY</td>
        <td>A flag indicating if the currencySymbol will be used</td>
    </tr>
    <tr>
        <td>negativeParenthesis</td>
        <td>JST_DEFAULT_NEGATIVE_PARENTHESIS</td>
        <td>A flag indicating if, when the number is negative, surround it with parenthesis (true) use a minus sign (false)</td>
    </tr>
    <tr>
        <td>groupSize</td>
        <td>JST_DEFAULT_GROUP_SIZE</td>
        <td>The number of integer digits to group. Normally 3 - thousands</td>
    </tr>
    <tr>
        <td>spaceAfterCurrency</td>
        <td>JST_DEFAULT_SPACE_AFTER_CURRENCY</td>
        <td>A flag indicating if a space will be rendered after the currency symbol</td>
    </tr>
    <tr>
        <td>currencyInside</td>
        <td>JST_DEFAULT_CURRENCY_INSIDE</td>
        <td>A flag indicating if, when the number is negative, the currency symbol will appear inside the minus sign or parenthesis (true) or outside (false)</td>
    </tr>
</table>
<p>When an invalid string is passed to the parse method the result is null.</p>
</p>This parser also has the round method, that takes a number and rounds it according to the
parser decimalDigits (when it's -1, no rounding is performed).</p>
<p>Here are some examples:</p>
<pre>var parser = new NumberParser();
parser.decimalDigits = 2;
parser.decimalSeparator = ",";
parser.groupSeparator = "."; 
parser.useGrouping = true; 

parser.parse("123,9") -> 123.9
parser.parse("123,917") -> 123.92 (round up)
parser.parse("9.876") -> 9876
parser.parse("ABC") -> null
parser.format(123456.781) -> "123.456,78"
parser.format(123456.789) -> "123.456,79" (round up)
parser.round(123456.789) -> 123456.79 (round up)

//Now we will use some attributes on the constructor
var parser2 = new NumberParser(2, ",", ".", true, "$", true);
parser2.format(-9876.5432) -> "$ -9.876,54"
parser2.negativeParenthesis = true;
parser2.format(-9876.5432) -> "$ (9.876,54)"
parser2.currencyInside = true;
parser2.format(-9876.5432) -> "($ 9.876,54)"
</pre>
<br><br>
<hr>
<table width="100%">
    <tr>
        <td width="33%" align="left"><b>Previous:</b><br><a href="Parsers_intro.html">Introduction to Parsers</a></td>
        <td width="34%" align="center"><a href="index.html">Table of Contents</a></td>
        <td width="33%" align="right"><b>Next:</b><br><a href="Parsers_date.html">Working with the DateParser</a></td>
    </tr>
</table>

</body>
</html>
